package cn.aopmin.utils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * 存储用户 ID 的 ThreadLocal
 * @author 白豆五
 * @since 2025/3/12
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE) // 构造器私有化
public class UserContextHolder {

    private static final ThreadLocal<Long> USER_ID_THREAD_LOCAL = new ThreadLocal<>();

    /**
     *  设置用户ID
     * @param userId 用户id
     */
    public static void setUserId(Long userId) {
        UserContextHolder.USER_ID_THREAD_LOCAL.set(userId);
    }

    /**
     *  获取用户ID
     * @return 用户ID
     */
    public static Long getUserId() {
        return USER_ID_THREAD_LOCAL.get();
    }

    /**
     *  移除用户ID
     */
    public static void clear() {
        USER_ID_THREAD_LOCAL.remove();
    }
}